package cn.org.xiaosheng.okhttp;

import cn.org.xiaosheng.hutool.utils.PostUtils;
import cn.org.xiaosheng.okhttp.utils.PostOkHttpUtils;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author XiaoSheng
 * @date 2023-11-17
 * @dec 描述
 */
@RestController
public class PostOkHttpTest {

    @Resource
    private PostOkHttpUtils postOkHttpUtils;

    @RequestMapping("/testOkPost1")
    public String testOkPost1() {
        HashMap<String, String> bodyMap = new HashMap<>();
        bodyMap.put("agentId", "eba5652793ee462f8cf2e78109e49326");
        bodyMap.put("deviceid", "3C6A2CFFFED345C4");
        bodyMap.put("accessToken", "2AHGxxxKp");
        bodyMap.put("topic", "api/device/3C6A2CFFFED345C4/cloud");
        bodyMap.put("dtype", "1");
        bodyMap.put("ts", "586851710");
        Map<String, Object> sData = new HashMap<String, Object>();
        sData.put("type","zigbeeConfig");
        sData.put("command", "epList");
        sData.put("session", "");
        sData.put("sequence", "1024");
        bodyMap.put("sdata", JSONObject.toJSONString(sData));
        // 可以不需要传递请求头, 会根据你传入的川参加进行判断,从而确定是哪种请求头
        JSONObject jsonObject = postOkHttpUtils.postOkHttpByJson("http://xxxx/owon-data/5000/agent/sendMqttData", bodyMap, null);
        return jsonObject.toJSONString();
    }

    /**
     * 测试post请求, 入参方式为form-data或者application/x-www-form-urlencoded方式
     * @return
     */
    @RequestMapping("/postOKHttpTest")
    public String postTest() {
        HashMap<String, Object> bodyMap = new HashMap<>();
        bodyMap.put("appKey", "bee95af85xxxxddc0f1e");
        bodyMap.put("appSecret", "ddc779bxxxx0bb510");
        HashMap<String, String> headers = new HashMap<>();
        headers.put("Content-Type", "application/x-www-form-urlencoded");
        JSONObject post = postOkHttpUtils.postOkHttpByFormData("https://xxxxx/api/lapp/token/get", bodyMap, headers);
        System.out.println(post);
        return JSONObject.toJSONString(post);
    }

}
